package cn.dengta.webapp.user.entity.moment;

import java.io.Serializable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;

/**
 * @author lizhichao
 * @since 2024-04-24
 */
@Setter
@Getter
public class MomentExt implements Serializable {

	/** 扩展类型 */
	protected ExtType type;

	protected JSONObject data;

	public static MomentExt of(@Nonnull ExtType type, @Nonnull ExtData data) {
		if (data.getClass() != type.dataClazz) {
			throw new IllegalArgumentException("data class must be " + type.dataClazz);
		}
		MomentExt ext = new MomentExt();
		ext.setType(type);
		ext.setData((JSONObject) JSONObject.toJSON(data));
		return ext;
	}

	/**
	 * 获取扩展数据
	 */
	@Nullable
	public ExtData data() {
		if (data == null) {
			return null;
		}
		return data.toJavaObject(type.dataClazz);
	}

}
